package com.yx.frame.builder.code;

public class MobileControls {
	JsFileWriter writer=null;
	String jsControlName;
	public MobileControls(String jsControlName,JsFileWriter writer) throws Exception{
		this.writer=writer;
		this.jsControlName=jsControlName;
		setLocalStorage();
		getLocalStorage();
		setSessionStorage();
		getSessionStorage();
		fileUploader();		
	}
	public void fileUploader()throws Exception{
		StringBuffer sb=new StringBuffer();
		sb.append("	"+jsControlName+".mFileUploader(fileName,function(isOK,message,fileUrls){<br>\r\n"); 
		sb.append("	    &nbsp;&nbsp;if(isOK){//服务调用成功,从fileUrls数组中取出服务器存储文件路径<br>\r\n");
		sb.append("	    &nbsp;&nbsp;}else{//服务调用失败，返回错误信息<br>\r\n");
		sb.append("	        &nbsp;&nbsp;&nbsp;&nbsp;var errorMessage=message;<br>\r\n");
		sb.append("	    &nbsp;&nbsp;}<br>\r\n");
		sb.append("	});\r\n");
		
		writer.html("true","", "mFileUploader(fileName, callbackFunction)", 
				"Hbuilder框架下手机版文件上传服务，服务器端文件存储位置需要根据具体情况通过接口com.yx.frame.server.Provider实现",
				"系统", "", "fileName：String文件路径", "返回数据存储在标签FileUrls中，存储方式为数组",	sb.toString());
		writer.write("true","	this.mFileUploader=function(fileName, callbackFunction) {");
		writer.write("true","if(!fileName){");
		writer.write("true","callbackFunction(false,\"文件为空不能上传\"); ");
		writer.write("true","return;");
		writer.write("true","}");
		writer.write("true","var url = this.serverUrl + \"FileUpload\";");
		//writer.write("true","var timestamp = (new Date()).valueOf();");
		writer.write("true","var waitIcon=plus.nativeUI.showWaiting();");
		writer.write("true","var task = plus.uploader.createUpload(url, {method: \"POST\"}, function(t,status){");
		writer.write("true","if(status==200){");
		writer.write("true","var returnString=t.responseText.substring(30,t.responseText.indexOf(\"</script>\"));");
		writer.write("true","var jsonData=JSON.parse(returnString);");
		writer.write("true","callbackFunction(jsonData.IsOK,jsonData.Message,jsonData.FileUrl);");
		writer.write("true","}else{");
		writer.write("true","callbackFunction(false,\"上传失败：\"+status);"); 
		writer.write("true","}");
		writer.write("true","waitIcon.close();");
		writer.write("true","});");
		writer.write("true","task.addData(\"UId\",this.getUID());");
		writer.write("true","task.addFile(fileName, {key: fileName.substr(fileName.lastIndexOf('/')+1)});");
		writer.write("true","task.start();");
		writer.write("true","};\r\n");
	}	
	public void setLocalStorage()throws Exception{
		StringBuffer sb=new StringBuffer();
		sb.append("	"+jsControlName+".setLocalStorage(key,jsonValue);\r\n");
		writer.html("true","", "setLocalStorage(key,jsonValue)", "数据本地磁盘存储", "系统", "", "key:String,数据主键；jsonValue:JSON数据", "无", sb.toString());
		writer.write("true","	this.setLocalStorage=function(key,jsonValue){localStorage.setItem(key, JSON.stringify(jsonValue));};\r\n");		
	}
	public void setSessionStorage()throws Exception{
		StringBuffer sb=new StringBuffer();
		sb.append("	"+jsControlName+".setSessionStorage(key,jsonValue);\r\n");
		writer.html("true","", "setSessionStorage(key,jsonValue)", "数据本地内存缓存", "系统", "", "key:String,数据主键；jsonValue:JSON数据", "无", sb.toString());
		writer.write("true","	this.setSessionStorage=function(key,jsonValue){sessionStorage.setItem(key, JSON.stringify(jsonValue));};\r\n");		
	}
	public void getLocalStorage()throws Exception{
		StringBuffer sb=new StringBuffer();
		sb.append("	"+jsControlName+".getLocalStorage(key);\r\n");
		writer.html("true","", "getLocalStorage(key)", "取得数据本地磁盘存储", "系统", "", "key:String,数据主键；", "jsonValue:JSON数据", sb.toString());
		writer.write("true","	this.getLocalStorage=function(key){return JSON.parse(localStorage.getItem(key) || \"{}\");};\r\n");		
	}
	public void getSessionStorage()throws Exception{
		StringBuffer sb=new StringBuffer();
		sb.append("	"+jsControlName+".getSessionStorage(key);\r\n");
		writer.html("true","", "getSessionStorage(key)", "取得数据本地内存缓存", "系统", "", "key:String,数据主键；", "jsonValue:JSON数据", sb.toString());
		writer.write("true","	this.getSessionStorage=function(key){return JSON.parse(sessionStorage.getItem(key) || \"{}\");};\r\n");		
	}
	
}
